# -*- coding: utf-8 -*-
# Date: 2025/6/21 10:51

from utils.request_util import RequestHelper
from config.logging_config import logger

# 通用检查
boss_account_data_ckeck_cache_prefix = "boss_account_data_ckeck_cache_prefix|"
# 通用记账
boss_account_data_supple_cache_prefix = "boss_account_data_supple_cache_prefix|"
# 政府预算批准数据检查
boss_reply_account_data_ckeck_cache_prefix = "boss_reply_account_data_ckeck_cache_prefix|";
# 政府预算批准数据记账
boss_reply_account_data_supple_cache_prefix = "boss_reply_account_data_supple_cache_prefix|"


def get_key_cache(request_helper, url_template, key):
    url = url_template.format(key=key)
    resp = request_helper.get_request(url)
    res = resp.json()["data"]
    if res is not None:
        print(key + ",结果:" + str(res))
    return tuple(str(res).split("/"))

def start_progress(host,tokenId,cache_prefix):
    headers = {}
    headers["Tokenid"] = tokenId
    request_helper = RequestHelper(host, headers)

    url_template = "/bgt-babudget-service/v2/redis/queryByKey?key={key}"
    url_process_cache_template = "/bgt-babudget-service/v2/bgt/suppleaccount/getProcessCache?prefix={prefix}"

    url_process_cache = url_process_cache_template.format(prefix=cache_prefix)
    resp = request_helper.get_request(url_process_cache)

    code = resp.json()["code"]
    if 100000 != code:
        print(resp.json()["message"])
        return

    redis_key_list = resp.json()["data"]
    if redis_key_list is None or len(redis_key_list) == 0:
        print("暂无缓存key集合数据")
        return

    total = 0
    successCount = 0

    for key in redis_key_list:
        if "progress" in key or "lock" in key:
            continue
        res = get_key_cache(request_helper, url_template, key)
        if cache_prefix == boss_account_data_supple_cache_prefix or cache_prefix == boss_account_data_ckeck_cache_prefix:
            total = total + int(res[1])
            successCount = successCount + int(res[0])

    get_key_cache(request_helper, url_template, cache_prefix + "progress")
    get_key_cache(request_helper, url_template, cache_prefix + "lock")
    print(f"本级请求处理数据总数：{total},成功数据个数:{successCount},失败数据个数：{total - successCount}")

if __name__ == '__main__':
    tokenId = "c0c529d4c84787dcf0ead409aa08ef9e"

    host_sc = "http://59.198.240.167:6001"
    host_zsc = "http://172.24.24.24:6001"

    cache_prefix = boss_reply_account_data_ckeck_cache_prefix

    start_progress(host_zsc,tokenId,cache_prefix)